<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            height: 50px;
            line-height: 50px;
            text-align: center;
            background: -webkit-linear-gradient(left top, yellow, yellowGreen);
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>

</html>
<script>
    let clockBox = document.getElementsByClassName("box")[0];
    // 目标时间
    let targetTime = "2021-10-10 13:00:00";
    function cutDown(targetTime) {
        
        // 时间差
        let diffTime = (new Date(targetTime) - new Date()) / 1000;// 得到时间戳  
        if(diffTime<0){
           clearInterval(time);
           time=null;
          // console.log(1);
           return "已过目标时间！";

        }
        let hour = Math.floor(diffTime / 3600);
        let minutes = Math.floor((diffTime - hour * 3600) / 60);
        let seconds = Math.floor(diffTime - hour * 3600 - minutes * 60);
        return addZero(hour) + "时" +addZero( minutes) + "分" + addZero(seconds) + "秒";
    }
   
    function addZero(n) {
        return n < 10 ? "0" + n : n;
    }
    clockBox.innerHTML = cutDown(targetTime);
  var time=setInterval(()=>{
        clockBox.innerHTML = cutDown(targetTime);
    },1000)
</script>